package Control;



import java.util.ArrayList;
import java.util.Iterator;

import javax.swing.JOptionPane;

import Entity.Extention_Stage_req;
import Entity.MsgPackage;
import GUI.ExtensionRequest_GUI;
import GUI.ExtentionReqManger_GUI;
import GUI.ScreenManager;
import GUI.ViewExtentionReq_GUI;

public class ExtensionRequestSuperV_Control {

	private ExtentionReqManger_GUI first_gui;
	private ViewExtentionReq_GUI second_gui;
	private MsgPackage exte_msg;
	private ArrayList<Extention_Stage_req> result;
	
	public ExtensionRequestSuperV_Control(){
		first_gui = new ExtentionReqManger_GUI();
		second_gui = new ViewExtentionReq_GUI();
	    exte_msg = new MsgPackage();
	    
	}
	public ArrayList<Extention_Stage_req> getExReq(){
		exte_msg.setOp_code(OP_CODE.GET_EX_REQ);
		exte_msg.setObject(null);
		ScreenManager.getConnection().accept(exte_msg);
		
		//while(ScreenManager.getConnection().getAnswerReceivedFlag() == false);	//wait until flag is changed and shows that result was received from server
		Object o = ScreenManager.getConnection().cclient.getMessage();

		//Object o = ScreenManager.getConnection().getResultobject();
		 result = (ArrayList<Extention_Stage_req>)o;
		return result;}
		
		public Extention_Stage_req extenSubmit_pressed(String req_id){
			
			Integer request_num=new Integer(Integer.parseInt(req_id));
			Iterator<Extention_Stage_req> it = result.iterator();
			Extention_Stage_req ex_r;
			while(it.hasNext()){
				ex_r=it.next();
				if(ex_r.getR_id().equals(request_num))
					//result.clear();
					return ex_r;
			}
		return null;
	}
		public void extend_request(String r_id,String stage_name,String num_days) {
			Extention_Stage_req extension_req= new Extention_Stage_req();
//set stage number			
			
	//set request number+number of days to extend
			extension_req.setR_id(Integer.parseInt(r_id));
			extension_req.setNumDay(Integer.parseInt(num_days));
			extension_req.setStage_id(get_stage_num(stage_name));

			exte_msg.setOp_code(OP_CODE.EXTEND_REQ);
			exte_msg.setObject(extension_req);
			ScreenManager.getConnection().accept(exte_msg);
			
			//while(ScreenManager.getConnection().getAnswerReceivedFlag() == false);	//wait until flag is changed and shows that result was received from server
			Object o = ScreenManager.getConnection().cclient.getMessage();
            Boolean b=(Boolean)o;
           if(!b){
   			JOptionPane.showMessageDialog(null, "The system was unable to extend the request", "Error", JOptionPane.ERROR_MESSAGE);		
           }
           else{
      			JOptionPane.showMessageDialog(null, "The stage was extended successfully", "", JOptionPane.INFORMATION_MESSAGE);		
 
           }
            	
           
			
		}
		
		
		public void reject_request(String r_id,String stage_name,String num_days) {
			Extention_Stage_req extension_req= new Extention_Stage_req();
//set stage number			
			
	//set request number+number of days to extend
			extension_req.setR_id(Integer.parseInt(r_id));
			extension_req.setNumDay(Integer.parseInt(num_days));
			extension_req.setStage_id(get_stage_num(stage_name));

			exte_msg.setOp_code(OP_CODE.REJECT_REQ);
			exte_msg.setObject(extension_req);
			ScreenManager.getConnection().accept(exte_msg);
			
			//while(ScreenManager.getConnection().getAnswerReceivedFlag() == false);	//wait until flag is changed and shows that result was received from server
			Object o = ScreenManager.getConnection().cclient.getMessage();
            Boolean b=(Boolean)o;
           if(!b){
   			JOptionPane.showMessageDialog(null, "The system was unable to reject the request", "Error", JOptionPane.ERROR_MESSAGE);		
           }
           else{
      			JOptionPane.showMessageDialog(null, "The extension request was updated successfully", "", JOptionPane.INFORMATION_MESSAGE);		
 
           }
            	
           
			
		}
		/**
		 * this method will return the stage id by his name
		 * @param stage_na the name of the stage
		 * @return stage number
		 */
		
		public Integer get_stage_num(String stage_na){
			Integer stage_num=null;
			if(stage_na.equals("estimation")){
				 stage_num=new Integer(1);
				
			}
			else if(stage_na.equals("examination")){
				 stage_num=new Integer(2);
				
			}
			else if(stage_na.equals("performance")){
				 stage_num=new Integer(3);
				
			}
			else if(stage_na.equals("testing")){
				 stage_num=new Integer(4);
				
			}
			else if(stage_na.equals("closure")){
				 stage_num=new Integer(5);
				
			}
			return stage_num;
			
		}
	/**
	 * getters and setters
	 * @return
	 */
		
		public ExtentionReqManger_GUI getfirst_gui() {
		return first_gui;
	}
	public void setfirst_gui(ExtentionReqManger_GUI first_gui) {
		this.first_gui = first_gui;
	}
	public ViewExtentionReq_GUI getsecond_gui() {
		return second_gui;
	}
	public void setsecond_gui(ViewExtentionReq_GUI second_gui) {
		this.second_gui = second_gui;
	}
	public MsgPackage getExte_msg() {
		return exte_msg;
	}
	public void setExte_msg(MsgPackage exte_msg) {
		this.exte_msg = exte_msg;
	}
	public ArrayList<Extention_Stage_req> getResult() {
		return result;
	}
	public void setResult(ArrayList<Extention_Stage_req> result) {
		this.result = result;
	}
	
	
	
}
